﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OOLib
{
   /// <summary>
   /// This service is the implementation of the reflection API. You can obtain information about types, 
   /// modify values of reflected types and call on objects.
   /// </summary>
   public class CoreReflection : Component
   {
      #region Constructors

      /// <summary>
      /// Constructor
      /// </summary>
      /// <remarks>
      /// After using this constructor, the ComObj must be set manually!
      /// </remarks>
      public CoreReflection() :
         base()
      { }

      /// <summary>
      /// Constructor
      /// </summary>
      /// <param name="comObject">COM object</param>
      public CoreReflection(object comObject) :
         base(comObject)
      { }

      #endregion

      #region XIdlReflection

      /// <summary>
      /// Obtaining a reflection interface for a type. You specify the type by its name. 
      /// If the given type name can not be reflected, then a null-reference is returned.
      /// </summary>
      /// <param name="typeName">The type's name </param>
      /// <returns>Returns the reflection interface for the demanded type (or null) </returns>
      public object ForName(string typeName)
      {
         CheckDisposed();
         return InvokeMethod("forName", typeName);
      }

      /// <summary>
      /// Obtaining a reflection interface for an object. This method takes the type of the object the any contains into account. 
      /// If the any contains no object, then a null-reference is returned. 
      /// </summary>
      /// <param name="comObject">An object</param>
      /// <returns>Returns the reflection interface of the type of the demanded object (or null)</returns>
      public object GetType(object comObject)
      {
         CheckDisposed();
         return InvokeMethod("getType", comObject);
      }

      #endregion

      #region Utility functions

      /// <summary>
      /// Creates a named property and assigns a value.
      /// </summary>
      /// <param name="name">Property name</param>
      /// <param name="value">Property value</param>
      /// <returns>Return a new property.</returns>
      public object CreateProperty(string name, object value)
      {
         CheckDisposed();

         var fn = ForName("com.sun.star.beans.PropertyValue");
         var args = new object[] { null };
         InvokeMethodOf(fn, "createObject", args, new[] { true });

         SetPropertyOf(args[0], "Name", name);
         SetPropertyOf(args[0], "Value", value);

         return args[0];
      }

      #endregion
   }
}
